package fun.sanguo.service.record.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import fun.sanguo.mapper.record.RecordPeriodMapper;
import fun.sanguo.model.dto.PageInfo;
import fun.sanguo.model.dto.PageInsert;
import fun.sanguo.model.dto.PageQuery;
import fun.sanguo.model.dto.PageUpdate;
import fun.sanguo.model.dto.record.RecordPeriodInsert;
import fun.sanguo.model.dto.record.RecordPeriodParam;
import fun.sanguo.model.dto.record.RecordPeriodUpdate;
import fun.sanguo.model.entity.record.RecordPeriodEntity;
import fun.sanguo.model.enums.ResEnums;
import fun.sanguo.service.record.RecordPeriodService;
import fun.sanguo.utils.DateUtils;
import fun.sanguo.utils.IdUtils;
import fun.sanguo.utils.exception.ArtificialException;

import javax.annotation.Resource;
import java.util.List;

/**
 * 内容管理-时期列表 Service实现类
 *
 * @author ZYP
 * @date 2023-04-25
 */
@Service
public class RecordPeriodServiceImpl implements RecordPeriodService {

    @Resource
    private RecordPeriodMapper recordPeriodMapper;

    /**
     * 查询页面table列表
     *
     * @param pageQuery
     * @return
     * @throws ArtificialException
     */
    @Override
    public PageInfo<RecordPeriodEntity> getPage(PageQuery<RecordPeriodParam> pageQuery) throws ArtificialException {
        // 查询参数
        RecordPeriodEntity recordPeriodEntity = new RecordPeriodEntity();
        // 转换查询参数
        try {
            // 接收参数
            RecordPeriodParam recordPeriodParam = new ObjectMapper().convertValue(pageQuery.getParam(), RecordPeriodParam.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordPeriodParam, recordPeriodEntity);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 查询数据并封装返回
        try {
            // 查询总量
            int total = recordPeriodMapper.getTotal(recordPeriodEntity);
            // 查询列表（分页）
            List<RecordPeriodEntity> recordPeriodEntityList = recordPeriodMapper.getList(recordPeriodEntity, pageQuery);
            // 封装并返回页面信息
            return new PageInfo<>(recordPeriodEntityList, total, pageQuery);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 新增
     *
     * @param pageInsert
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean insert(PageInsert<RecordPeriodInsert> pageInsert) throws ArtificialException {
        // 新增参数
        RecordPeriodEntity recordPeriodEntity = new RecordPeriodEntity();
        // 转换新增参数
        try {
            // 接收参数
            RecordPeriodInsert recordPeriodInsert = new ObjectMapper().convertValue(pageInsert.getParam(), RecordPeriodInsert.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordPeriodInsert, recordPeriodEntity);
            // 其他参数赋值
            recordPeriodEntity.setId(String.valueOf(new IdUtils().nextId()));
            recordPeriodEntity.setCreateBy("system");
            recordPeriodEntity.setCreateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordPeriodEntity.setUpdateBy("system");
            recordPeriodEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordPeriodEntity.setVersion(1);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 新增数据并得到操作结果
        try {
            // 新增
            int amount = recordPeriodMapper.insert(recordPeriodEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 编辑
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean update(PageUpdate<RecordPeriodUpdate> pageUpdate) throws ArtificialException {
        // 编辑参数
        RecordPeriodEntity recordPeriodEntity = new RecordPeriodEntity();
        // 转换编辑参数
        try {
            // 接收参数
            RecordPeriodUpdate recordPeriodUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordPeriodUpdate.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordPeriodUpdate, recordPeriodEntity);
            // 其他参数赋值
            recordPeriodEntity.setUpdateBy("system");
            recordPeriodEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 编辑数据并得到操作结果
        try {
            // 编辑
            int amount = recordPeriodMapper.update(recordPeriodEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 删除
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean delete(PageUpdate<RecordPeriodUpdate> pageUpdate) throws ArtificialException {
        // 删除参数
        RecordPeriodEntity recordPeriodEntity = new RecordPeriodEntity();
        // 转换删除参数
        try {
            // 接收参数
            RecordPeriodUpdate recordPeriodUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordPeriodUpdate.class);
            // 赋值到实体类
            recordPeriodEntity.setId(recordPeriodUpdate.getId());
            // 其他参数赋值
            recordPeriodEntity.setUpdateBy("system");
            recordPeriodEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 删除数据并得到操作结果
        try {
            // 删除
            int amount = recordPeriodMapper.delete(recordPeriodEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }
}
